Actions
createItem
This action lets you create a new item for a specified model. You can access this action via the useModelActions hook.
import { useModelActions } from '@rexlabs/model-generator';
function Component() {
const { createItem } = useModelActions(propertiesModel);
const handleCreate = (newProperty: Property) => {
createItem({data: newProperty})
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
data | EntryData | |
args | any | arguments to send along with the entry data |
Returns:
Promise<AxiosResponse<ModelEntry>>
fetchList
This action fetches a list by id, which means it must be present in the store already.
import { useModelActions } from '@rexlabs/model-generator';
function Component() {
const { fetchList } = useModelActions(propertiesModel);
const handleFetchList = () => {
fetchList({id: 'list-id'})
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
id | string | |
args | any | arguments to send along the fetch request |
Returns:
Promise<AxiosResponse<ModelEntry[]>>
fetchItem
This action fetches an item by id, which means it must be present in the store already.
import { useModelActions } from '@rexlabs/model-generator';
function Component({propertyId}: ComponentProps) {
const { fetchItem } = useModelActions(propertiesModel);
const handleFetchItem = () => {
fetchItem({id: propertyId})
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
id | string | |
args | any | arguments to send along the fetch request |
Returns:
Promise<AxiosResponse<ModelEntry>>
updateItem
This action updates a specific item.
import { useModelActions } from '@rexlabs/model-generator';
function Component({property}: ComponentProps) {
const { updateItem } = useModelActions(propertiesModel);
const handleUpdateItem = (updatedProperty: Partial<Property>) => {
updateItem({id: propertyId, data: updatedProperty})
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
id | string | |
data | Partial\<EntryData> | a partial to update the item with |
args | any | arguments to send along the fetch request |
Returns:
Promise<AxiosResponse<ModelEntry>>
refreshList
This action fetches a fresh state of a list. Usually helpful after having manipulated another list that has some related data.
import { useModelActions } from '@rexlabs/model-generator';
function Component({listId}: ComponentProps) {
const { refreshList } = useModelActions(propertiesModel);
const handleRefreshList = () => {
refreshList({id: listId})
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
id | string | |
args | any | arguments to send along the fetch request, usually the includes in this case |
Returns:
Promise<AxiosResponse>
refreshLists
This action refreshes all list in the store. Most helpful when used in the context of a particular model. Using this action you can easily refresh all lists associated with a model.
import { useModelActions } from '@rexlabs/model-generator';
function Component() {
const { refreshLists } = useModelActions(propertiesModel);
const handleRefreshLists = () => {
// refreshes all lists based on the propertiesModel
refreshLists()
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
args | any | arguments to send along the fetch request |
Returns:
Promise<AxiosResponse>
trashItem
trashItem lets you delete an entry with a specific id.
import { useModelActions } from '@rexlabs/model-generator';
function Component({propertyId}: ComponentProps) {
const { trashItem } = useModelActions(propertiesModel);
const deleteProperty = () => {
trashItem({id: propertyId})
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
id | string | id of the entry you want to delete |
args | any | arguments to send along the fetch request |
Returns:
Promise<AxiosResponse>
refreshItem
The refreshItem action lets you fetch a fresh copy of one specific entry in the store.
import { useModelActions } from '@rexlabs/model-generator';
function Component({propertyId}: ComponentProps) {
const { refreshItem } = useModelActions(propertiesModel);
const refreshProperty = () => {
refreshItem({id: propertyId})
}
...
}
Parameters: Object
| Name | Type | Description |
|---|---|---|
id | string | id of the entry you want to refresh |
args | any | arguments to send along the fetch request |
Returns:
Promise<AxiosResponse<ModelEntry>>